class Tank extends GridBase {
	/**血量 */
	public hp: number = 1;
	public team: Team = Team.Owner;
	public type: number = 1;
	public isRed: boolean = false;
	public isDead: boolean = false;
	public shootCD: number = 300;
	public bulletAlive = false;
	public maxLife: number = 1;
	public lifeCount: number = 0;

	private rebornComplete = false;
	private speed: number = gridValue;
	private lastDir: TankDir;
	private invincible = false;

	public constructor() {
		super();
		this.gridH = 2;
		this.gridV = 2;
		this.offsetH = 0;
		this.offsetV = 0;
	}
	/**死亡 */
	dead() {
		this.isDead = true;
		this.playEff(TankEff.effName.boom, () => {
			this.clear();
			if (this.team == Team.Owner) {
				this.lifeCount++;
				// if (this.lifeCount <= this.maxLife) {
				// 	//TODO:我方坦克复活


				// } else {
				// 	TankMap.ins.gameover();
				// }
				TankMap.ins.gameover();
			}
		}, 1);
	}
	/**重生*/
	reborn(f: Function) {
		this.invincible = true;
		this.playEff(TankEff.effName.reborn, () => {
			this.invincible = false;
			this.rebornComplete = true;
			f && f();
		}, 3);
	}
	/**受到攻击 */
	unHit() {
		if (this.invincible) return;
		if (--this.hp <= 0) return this.dead();
		if (this.isRed) {
			this.onRedTreasure();
			this.isRed = false;
		}
		this.update();
	}

	/**掉落宝物 */
	onRedTreasure() {

	}
	/**移动
	 * @param dir 方向
	 */
	move(dir: TankDir) {
		if (!this.rebornComplete) return -1;
		let x = this.x, y = this.y;
		switch (dir) {
			case TankDir.Up:
				y -= this.speed;
				break;
			case TankDir.Down:
				y += this.speed;
				break;
			case TankDir.Left:
				x -= this.speed;
				break;
			case TankDir.Right:
				x += this.speed;
				break;
			default:
				break;
		}

		if (this.lastDir != dir) {
			this.lastDir = dir;
			this.dir = dir;
		}

		if (this.stateBoundCheck(x, y) || this.mapBoundCheck(dir)) return false;
		this.x = x, this.y = y;
		return true;
	}
	private stateBoundCheck(x, y) {
		if (x < 0 || x > 830 - this.width / 2 || y < 0 || y > 640 - this.height / 2) {
			return true;
		}
	}
	private mapBoundCheck(dir: TankDir) {
		let gridList = this.getSideGrid(dir, 1);
		for (let grid of gridList) {
			let ele = MapElement.getElement(grid);
			if (ele && ele.isBlock()) {
				return true;
			}
		}
	}
	set dir(dir: TankDir) {
		this.img.rotation = dir * 90 | 0;
	}
	get dir() {
		let rotation = this.img.rotation;
		if (rotation < 0) rotation = rotation + 360;
		return rotation / 90 | 0;
	}
	/**获取子弹出生地点 */
	getShootPoint() {
		let x = this.x, y = this.y;
		switch (this.dir) {
			case TankDir.Up:
				x += this.width / 2;
				break;
			case TankDir.Down:
				x += this.width / 2;
				y += this.height;
				break;
			case TankDir.Left:
				y += this.height / 2;
				break;
			case TankDir.Right:
				y += this.height / 2;
				x += this.width;
				break;
			default:
				break;
		}
		return { x: x, y: y };
	}
	update() {
		if (this.team == Team.Owner) {
			this.img.source = TankData.tank1.levelSource[this.hp - 1] + "_png";
		} else {
			let tkdata = TankData["tank" + this.type];
			let source = this.isRed ? tkdata.redSouce : tkdata.levelSource[this.hp - 1];
			this.img.source = source + "_png";
		}
	}

	private shootTick: number = 0;
	set canShoot(b: boolean) {
		if (b == false) {
			this.shootTick = egret.getTimer();
		}
	}
	get canShoot() {
		let time = this.shootTick + this.shootCD;
		let time2 = egret.getTimer()
		return time <= time2 && !this.bulletAlive && this.rebornComplete;
	}
	shoot() {
		if (!this.canShoot) return;
		let bullet = Pool.getItem(Bullet);
		let {x, y } = this.getShootPoint();
		bullet.x = x, bullet.y = y, bullet.dir = this.dir;
		bullet.target = this;
		bullet.onDestory = () => { this.bulletAlive = false };
		this.canShoot = false;
		this.bulletAlive = true;
		TankMap.ins && TankMap.ins.addChild(bullet);
	}

	getTankData() {
		return TankData["tank" + this.type];
	}

	$onAddToStage(stage: egret.Stage, nestLevel: number) {
		if (this.team == Team.Other) {
			let data = this.getTankData();
			this.hp = data.level;
		} else {
			let {x, y } = MapElement.index2pos(1135);
			this.x = x, this.y = y;
			this.hp = 1;
		}
		this.dir = this.team == Team.Other ? TankDir.Down : TankDir.Up;
		this.isDead = false;
		this.rebornComplete = false;
		this.img.visible = false;
		this.update();
		this.reborn(() => {
			this.img.visible = true;
		})
		super.$onAddToStage(stage, nestLevel);
	}
}

/**方向 */
enum TankDir {
	Up,
	Right,
	Down,
	Left,
}
/**阵营 */
enum Team {
	Owner,
	Other
}


/**临时坦克数据 */
let TankData = {
	tank1: { levelSource: ["Image 35", "Image 51", "Image 54"], level: 1, speed: 100, life: 3 },
	tank2: { levelSource: ["Image 63"], redSouce: "Image 83", level: 1, speed: 300, life: 1 },
	tank3: { levelSource: ["Image 73"], redSouce: "Image 71", level: 1, speed: 200, life: 1 },
	tank4: { levelSource: ["Image 78", "Image 103", "Image 98"], redSouce: "Image 108", level: 3, speed: 350, life: 1 }
}